﻿#ifndef _THREAD_H_
#define _THREAD_H_

#include <thread>
#include <memory>
#include <vector>
#include <atomic>
#include <string>

class Runnable
{
public:
    virtual ~Runnable() = default;
    virtual void Run() = 0;
    void incReference() { ++m_refs; }
    void decReference()
    {
        if (!--m_refs)
            delete this;
    }
private:
    std::atomic_long m_refs{0};
};

class Thread
{
public:
    Thread();
    explicit Thread(Runnable* instance);
    ~Thread();

    bool wait();
    void destroy();

    static void Sleep(unsigned long msecs);
    static std::thread::id currentId();
    void Detach();
private:
    Thread(const Thread&);
    Thread& operator=(const Thread&);

    static void ThreadTask(void* param);

    Runnable* const m_task;
    std::thread::id m_iThreadId;
    std::thread m_ThreadImp;
};

#endif
